package com.vilinno.tlias.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.vilinno.tlias.mapper.ClazzMapper;
import com.vilinno.tlias.pojo.Clazz;
import com.vilinno.tlias.pojo.ClazzQueryParam;
import com.vilinno.tlias.pojo.Emp;
import com.vilinno.tlias.pojo.PageResult;
import com.vilinno.tlias.service.ClazzService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ClazzServiceImpl implements ClazzService {

    @Autowired
    private ClazzMapper clazzMapper;

    /**
     * 添加班级信息
     * @param clazz
     */
    @Override
    public void add(Clazz clazz){

        clazz.setCreateTime(LocalDateTime.now());
        clazz.setUpdateTime(LocalDateTime.now());

        clazzMapper.insert(clazz);

    }

    /**
     * 修改班级数据
     * @param clazz
     */
    @Override
    public void update(Clazz clazz){
        clazz.setUpdateTime(LocalDateTime.now());
        clazzMapper.update(clazz);
    }

    @Override
    public void delete(int id){

        clazzMapper.deleteById(id);

    }

    /**
     * 查询所有班级信息
     */
    @Override
    public List<Clazz> findAll(){
        return clazzMapper.findAll();
    }

    @Override
    public PageResult<Clazz> page(ClazzQueryParam clazzQueryParam){

        //1. 获取查询结果总记录

        PageHelper.startPage(clazzQueryParam.getPage(),clazzQueryParam.getPageSize());

        //2. 获取查询结果

        List<Clazz> rows=clazzMapper.list(clazzQueryParam);
        Page<Clazz> p=(Page<Clazz>)rows;

        //3. 封装结果

        return new PageResult<Clazz>(p.getTotal(),p.getResult());

    }

    @Override
    public Clazz searchById(Integer id){

        return clazzMapper.getById(id);

    }

}
